<?php

$user_id = $GLOBALS['user_id'];

$user = new user($user_id); 

$user_info[$user_id] = $user->load_tables('user_settings,user_stats,user_gladiator');

$estab_name = $user_info[$user_id]['establishment_name'];

$graphs = new graphs();

switch (@$_REQUEST['function'])
{
    case 'vntr':
                            if ($_REQUEST['v1'] == 'yes' && $user_info[$user_id]['glad_level'] < 1)
                            {
                                $sql = "UPDATE `user_gladiator` SET `glad_status` = 'trainee',`glad_level` = '0',`trainee_level`='1' WHERE `assoc_id` ='$user_id' LIMIT 1 ";
                                $res = $db->query($sql);
                                if ($res)
                                {
                                    $signup_fee = mt_rand(government_setting('gladiator_signup_min'),government_setting('gladiator_signup_max'));
                                    $result = transfer_money_gov2user(7,$user_id,$signup_fee,'quadrans');
                                    if ($result)
                                    {
                                        echo  '<p>Congratulations!<Br><BR> You are now a gladiator in training (<i>novicius</i>).';
                                        echo "The school has transferred $signup_fee quadrans to help you get started.";
                                        $options = array();
                                        $options['/gladiator/vntr/yes/'] = 'Yes, I agree to the auctoramentum and would like to become a gladiator';
                                        $options['/home/'] = 'No, thanks';
                                        echo '<br><br>Next?';
                                        echo player_options($options);
                                        echo '</p>';
                                    }
                                    else
                                    {
                                        echo 'Sorry, '.$GLOBALS['error_message'] . '.<BR><BR> You will need to try and join again when the school has more money to take new people on.';
                                    }
                                }
                            }
                            elseif ($_REQUEST['v1'] == 'yes' && $user_info[$user_id]['glad_level'] > 0)
                            {
                                $options = array();
                                $options['/home/'] = 'Go home';
                                echo '<p>You are already a gladiator, you can not rejoin.';
                                echo player_options($options);
                                echo '</p>';
                            }
                            else
                            {
                                $options = array();
                                $options['/gladiator/vntr/yes/'] = 'Yes, I agree to the auctoramentum and would like to become a gladiator';
                                $options['/home/'] = 'No, thanks';
                                echo '<p>Are you sure you want to volunteer to become a gladiator?';
                                echo player_options($options);
                                echo '</p>';
                            }
    break;
    
    
    
    
    
    
    
    
    
    
    case 'train':
                    $train = new training($user_info["$user_id"], $user_id); 

                     echo "<h1>$estab_name -&gt; Training</h1>";
                     $train_options = array();
                     $train_options['/gladiator/train/weights/'] = 'Weight Training';
                     echo '<p>What do ya want to do, little man?<BR>';
                     echo player_options($train_options);
                     echo '</p>';

                     $weight = @$_REQUEST['weight'];
                     $reps = @$_REQUEST['reps'];
                     $dur = @$_REQUEST['dur'];

                     
                     if ($_REQUEST['v1'] == 'weights' && @$_REQUEST['training'] > 0)
                     {
                        echo $train->weights_calculate_gain($weight,$reps,$dur);
                     }
                     elseif ($_REQUEST['v1'] == 'weights')
                     {
                        echo $train->weights_show_options();
                     }
    break;

    
    
    
    
    
    
    

    case 'arm':
                    $armoury = new gladiator_armoury($user_info["$user_id"], $user_id); 
                     if ($_REQUEST['v1'] == 'train' && @$_REQUEST['v2'] < 1)
                     {
                     
                        //  The Armoury
                         // THE first page that shows ALL weapons
                         // And bolds the ones that you have already learned
                         
                            echo '<p>Please choose from the list below which items you want to learn to use...';
                            echo '<h2>Weapons</h2>';
                            echo $armoury->list_items(0,'weapon',$user_info[$user_id]['gladiator_items_learned']);
                            echo '<br><h2>Armour</h2>';
                            echo $armoury->list_items(0,'armour',$user_info[$user_id]['gladiator_items_learned']);
                     }
                     elseif ($_REQUEST['v1'] == 'train' && @$_REQUEST['v2'] > 0 && @$_REQUEST['guess1'] > 0 && @$_REQUEST['guess2'] > 0 && @$_REQUEST['guess3'] > 0)
                     {
                        
                        // RESULTS for specific weapon training
                        // shows what you answered teh roman numeral questions to be
                        // adds that weapon to your account as learned if all three are correct
                        
                            $item_id = $_REQUEST['v2'];
                            $item_data = $db->return_first_result("SELECT * FROM `gladiator_items` WHERE `id`= '$item_id'");
                            echo '<h1>Training for: '.$item_data['name'].'</h1>';
                            echo '<p>'.$item_data['description'].'</p>';
                            echo '<h2>Results</h2>';
                            echo $armoury->answer($item_id,$item_data['question_1'],$item_data['question_2'],$item_data['question_3'],$_REQUEST['guess1'],$_REQUEST['guess2'],$_REQUEST['guess3'],$item_data['type']);
                     }
                     elseif ($_REQUEST['v1'] == 'train' && @$_REQUEST['v2'] > 0)
                     {
                        // SPECIFIC weapon training
                        // shows the three roman questions
                        // displays the form for the questions
                        
                            $item_id = $_REQUEST['v2'];
                            $item_data = $db->return_first_result("SELECT * FROM `gladiator_items` WHERE `id`= '$item_id'");
                            echo '<h1>Training for: '.$item_data['name'].'</h1>';
                            echo '<p>'.$item_data['description'].'</p>';
                            echo '<h2>Questions</h2>';
                            echo $armoury->question($item_data['question_1'],$item_data['question_2'],$item_data['question_3']);
                     }
                     elseif ($_REQUEST['v1'] == 'immob')
                     {
                        //  The immobilisation section
                         // shows all weapons and highlights the one the user has and enables an 'equip' option if they have it learned
                         
                         if (@$_REQUEST['v2'] > 0)
                         { 
                            echo $armoury->equip(USER_ID,$_REQUEST['v2'],$user_info[$user_id]['gladiator_items_learned'],$user_info[$user_id]['gladiator_items_inventory']); 
                            $user_info[$user_id] = $user->load_tables('user_settings,user_gladiator');
                         }
                         elseif (@$_REQUEST['v2'] == 'unequip')
                         {
                            echo $armoury->unequip(USER_ID,@$_REQUEST['v3']);
                            $user_info[$user_id] = $user->load_tables('user_settings,user_gladiator');
                         }
                         
                            echo '<p>Please choose from the list below which items you want to equip/immobolise/arm your self with...';
                            echo '<h2>Weapons</h2>';
                            echo $armoury->list_items(USER_ID,'weapon',$user_info[$user_id]['gladiator_items_learned'],$user_info[$user_id]['gladiator_items_inventory']);
                            echo '<br><h2>Armour</h2>';
                            echo $armoury->list_items(USER_ID,'armour',$user_info[$user_id]['gladiator_items_learned'],$user_info[$user_id]['gladiator_items_inventory']);
                     }
                     else
                     {
                        echo 'plain old armoury';
                     }
                     echo '</p>';
    break;

    
    
    
    
    
    
    


    case 'lib':
                     echo "<h1>$estab_name -&gt; Library</h1>";
                     echo '<p>';
                     
                     echo display_text('gladiator_lib');
                     
                     switch ($_REQUEST['v1'])
                     {
                     	case 'stats':
                     		if ($_REQUEST['v2'] == 'self')
                     		{
                     			// self battle stats
                     			$animal_wins = $user_info[USER_ID]['animal_wins'];
                     			$animal_losses = $user_info[USER_ID]['animal_losses'];
                     			$animal_draws = $user_info[USER_ID]['animal_draws'];
                     			
                     			$training_wins = $user_info[USER_ID]['train_wins'];
                     			$training_losses = $user_info[USER_ID]['train_losses'];
                     			$training_draws = $user_info[USER_ID]['train_draws'];
                     			
                     			$event_wins = $user_info[USER_ID]['event_wins'];
                     			$event_losses = $user_info[USER_ID]['event_losses'];
                     			$event_draws = $user_info[USER_ID]['event_draws'];
                     			
                     			$total_animal = $animal_wins + $animal_losses + $animal_draws;
                     			$total_training = $training_wins + $training_losses + $training_draws;
                     			$total_events = $event_wins + $event_losses + $event_draws;
                     			
                     			$total_battles = number_format($total_animal + $total_training + $total_events);
                     			
                     			$output = "<br><BR>You have been in <B>$total_battles</B> battles altogether.<br>

									<br>
									<h2><B>$total_events</B> main events</h2><br>
									<ul>
									  <li><B>$event_wins</B> won</li>
									  <li><B>$event_losses</B> lost</li>
									  <li><B>$event_draws</B> drawn</li>
									</ul>
									<br>
									<h2><B>$total_animal</B> with animals</h2><br>
									<ul>
									  <li><B>$animal_wins</B> won</li>
									  <li><B>$animal_losses</B> lost</li>
									  <li><B>$animal_draws</B> drawn</li>
									</ul>
									<br>
									<h2><B>$total_training</B> with people</h2><br>
									<ul>
									  <li><B>$training_wins</B> won</li>
									  <li><B>$training_losses</B> lost</li>
									  <li><B>$training_draws</B> drawn</li>
									</ul>
									

										";
                     			
                     			echo $output;
                     		}
                     		elseif ($_REQUEST['v2'] == 'public')
                     		{
                     			// public battle stats
                     			$query = "SELECT 
                     			SUM(animal_wins) AS animal_wins ,
                     			SUM(animal_losses) AS animal_losses ,
                     			SUM(animal_draws) AS animal_draws ,
                     			SUM(train_wins) AS train_wins ,
                     			SUM(train_losses) AS train_losses ,
                     			SUM(train_draws) AS train_draws ,
                     			SUM(event_wins) AS event_wins ,
                     			SUM(event_losses) AS event_losses ,
                     			SUM(event_draws) AS event_draws 
                     			FROM user_gladiator";
                     			$battle_data = $db->return_first_result($query);
                     			
                     			
      							$animal_wins = $battle_data['animal_wins'];
                     			$animal_losses = $battle_data['animal_losses'];
                     			$animal_draws = $battle_data['animal_draws'];
                     			
                     			$training_wins = $battle_data['train_wins'];
                     			$training_losses = $battle_data['train_losses'];
                     			$training_draws = $battle_data['train_draws'];
                     			
                     			$event_wins = $battle_data['event_wins'];
                     			$event_losses = $battle_data['event_losses'];
                     			$event_draws = $battle_data['event_draws'];
                     			
                     			$total_animal = $animal_wins + $animal_losses + $animal_draws;
                     			$total_training = ($training_wins + $training_losses)/2 + $training_draws;
                     			$total_events = ($event_wins + $event_losses)/2 + $event_draws;
                     			
                     			$total_battles = number_format($total_animal + $total_training + $total_events);
                     			
                     			$output = "<br><BR>There has been a grand total of <B>$total_battles</B> battles.<br>

									<br>
									<h2><B>$total_events</B> main events</h2><br>
									<ul>
									  <li><B>$event_wins</B> with a winner & loser</li>
									  <li><B>$event_draws</B> drawn</li>
									</ul>
									<br>
									<h2><B>$total_animal</B> with animals</h2><br>
									<ul>
									  <li><B>$animal_wins</B> won</li>
									  <li><B>$animal_losses</B> lost</li>
									  <li><B>$animal_draws</B> drawn</li>
									</ul>
									<br>
									<h2><B>$total_training</B> with people</h2><br>
									<ul>
									  <li><B>$training_wins</B> with a winner & loser</li>
									  <li><B>$training_draws</B> drawn</li>
									</ul>
									

										";
                     			
                     			echo $output;
                     		}
                     		break;
                     		
                     	case 'history':
                     		break;
                     }
                     echo '</p>';
    break;

    
    
    
    
    
    
    


    case 'games':
                     echo "<h1>$estab_name -&gt; Games</h1>";
                     echo '<p>';
                     echo '</p>';
    break;

    
    
    
    
    
    

    case 'fight':
    	if ($_REQUEST['v1'] == 'animals')
    	{
                    $fight = new gladiator_animals($user_info[$user_id],$user_id);
                     echo "<h1>$estab_name -&gt; Fight Arena</h1>";
                     echo '<p>';
                     
                     # Animal Fighting
                     # 1. select a breed & size
                     # 2. pick an area to attack
                     # 3. review hit results and repeat #4
                     
                     $breed = @$_REQUEST['v2'];
                     $attack = @$_REQUEST['v3'];
                     $energy = @$_POST['energy'];
                     $area = @$_POST['area'];
                     $distance = @$_POST['distance'];
                     
                     if (strlen($energy) > 0 && strlen($area) > 0 && strlen($distance) > 0)
                     {
                     	$attack = TRUE;
                     }
                     else
                     {
                     	$attack = FALSE;
                     }
                     
                     if (strlen($breed) == 0)
                     # Player has to pick an animal
                     {
                         // Show all animals
                         echo $fight->show_all_animals();
                     }
                     elseif ($breed && $attack == FALSE)
                     # Player has to select an area of the animal to attack
                     {
                         // show attack options
                         echo $fight->show_attack_options($breed);
                     }
                     elseif ($breed && $attack)
                     # Player has to view results and more attack options
                     {
                         // show attack results
                         echo $fight->show_attack($user_info[USER_ID],$breed,$energy,$area,$distance);
                         // show attack options
                         echo $fight->show_attack_options($breed,$energy,$area,$distance); 
                         
                     }
                     
                     
                     echo '</p>';
    	}
    	elseif ($_REQUEST['v1'] == 'people')
    	{
    		#
    		# Gladiator Training Battle
    		#
    		
    		$opponent_id = $_REQUEST['v2'];
    		
    		if ($opponent_id < 1)
    		{
    			$url = URL_PLAY . UID . '/psearch/';
    			header ("Location: $url");
    			exit();
    		}
    		
    		$area = @$_REQUEST['area'];
    		$distance = @$_REQUEST['distance'];
    		$energy_used = @$_REQUEST['energy'];
    		
    		$opponent = new user($opponent_id);
	    	$user_info[$opponent_id] = $opponent->load_tables('user_settings,user_stats,user_gladiator');
	    		
	    	$battle = new player_battle(USER_ID,$opponent_id);

    		if (strlen($area) > 0)
    		{
	    			
	    		# Check players are alive
	    		$result = $battle->check_alive();
	    		
	    		# Initiate/maintain the battle record in the database
	    		if ($result) { $result = $battle->initiate_battle(); }
	    		
	    		# Check which player is the fastest and store them as player_battle->fastest/slowest_player
	    		if ($result) 
	    		{ 
	    			$result = $battle->speed_check($distance); 
	    			
	    			# Store the 2 players in easy to remember variables
	    			$attacker = $user_info[$battle->fastest_player];
	    			$defender = $user_info[$battle->slowest_player];
	    		}
	    		
	    		# If all the above checks went ok then do the battle
	    		if ($result)
	    		{
	    			
	    			# Check to see if user attacking is the user browsing the site, 
	    			#  if not then the area of attack is just retaliation/defense as opposed to what the user selected
	    			if ($attacker['assoc_id'] <> USER_ID)
	    			{
	    				$area = 'defense';
	    				$energy_used = 10;
	    			}
	    			
	    		
	    			# Calculate the amount of points to deduct from the defending players energy
	    			$attacker['hit_power'] = $battle->calculate_hit_power($attacker,$area,$energy_used); 
	    			
	    			# Check to see the player who attacked never missed
	    			$hit_success = $battle->hit_success($area,$energy_used);
	
	    		
	    			# Calculate the amount of points to deduct from the attacking players hit
	    			$defender['defense_bonus'] = $battle->calculate_armour_points($defender);
	    			$defender['defense_bonus'] += $battle->calculate_reaction_bonus($area);
	    			
	    			# Total damage done
	    			$damage_done = $attacker['hit_power'] - $defender['defense_bonus'];
	    			
	    			# Set the remaining energy 
	    			$defender['remaining_energy'] = round($defender['energy'] - $damage_done);
	    			$attacker['remaining_energy'] = $attacker['energy'] - ($energy_used/10);
	    			
	    			# Display battle log, as of yet (if there is one)
	    			if ($battle->first_battle == 0)
	    			{
	    				$battle->battle_log();
	    			}
	
	    		
		    		if ($defender['remaining_energy'] > 0 && $defender['remaining_energy'] > 0 && $damage_done > 0 && $hit_success)
		    		{
		    			# Attacker was succesfull, Player survived the attack and the attack options should be displayed again
		    			$attacker_username = $attacker['username'];
		    			$defender_username = $defender['username'];
		    			$hit_power = $attacker['hit_power'];
		    			$remaining_energy = $defender['remaining_energy'];
		    			$battle_log = "$attacker_username hit $defender_username; \n  - taking $damage_done energy from him, \n  - leaving him with $remaining_energy energy!";
		    			$battle->battle_log($battle_log,$distance);
		    			$battle->update_energy($attacker['assoc_id'],$attacker['remaining_energy']);
		    			$battle->update_energy($defender['assoc_id'],$defender['remaining_energy']);
		    			$battle_end = FALSE;
		    		}
		    		elseif ($damage_done < 1 || $hit_success == FALSE)
		    		{
		    			# Attacker missed, Player survived the attack and the attack options should be displayed again
		    			$attacker_username = $attacker['username'];
		    			$defender_username = $defender['username'];
		    			$battle_log = "$attacker_username tried to hit $defender_username; \n  - and missed!";
		    			$battle->battle_log($battle_log,$distance);
		    			$battle_end = FALSE;
		    		}
		    		elseif ($defender['remaining_energy'] < 1) 
		    		{
		    			# Player died because of the attack
		    			$attacker_username = $attacker['username'];
		    			$defender_username = $defender['username'];
		    			$battle_log = "$attacker_username hit $defender_username; \n  - taking $damage_done energy from him, \n  - <b>killing him</b>!";
		    			$battle->battle_log($battle_log,$distance);
		    			$battle->update_energy($attacker['assoc_id'],$attacker['remaining_energy']);
		    			$battle->update_energy($defender['assoc_id'],$defender['remaining_energy']);
		    			$battle_end = $battle->end();
		    			$battle->add_win($attacker['assoc_id'],$attacker['points']);
		    			$battle->add_loss($defender['assoc_id']);
		    		}
		    		elseif ($attacker['remaining_energy'] < 1) 
		    		{
		    			# Player died because of the attack (the attacking player)
		    			$attacker_username = $attacker['username'];
		    			$defender_username = $defender['username'];
		    			$battle_log = "$defender_username defeated $attacker_username. ($attacker_username used to much energy in his last hit)";
		    			$battle->battle_log($battle_log,$distance);
		    			$battle->update_energy($attacker['assoc_id'],$attacker['remaining_energy']);
		    			$battle->update_energy($defender['assoc_id'],$defender['remaining_energy']);
		    			$battle_end = $battle->end();
		    			$battle->add_win($defender['assoc_id'],$attacker['points']);
		    			$battle->add_loss($attacker['assoc_id']);
		    		}
		    		else 
		    		{
		    			echo 'something went wrong at the end of the checks';
		    		}
	    		}
	    		else 
	    		{
	    			echo "$battle->error";
	    			$battle_end = TRUE;
	    		}
	    		
	    		# Check to see if the battle options should be shown again or not
	    		if ($battle_end == FALSE)
	    		{
	    			
	    			if ($attacker['assoc_id'] == USER_ID)
	    			{
	    				echo $battle->show_attack_options($energy_used,$area,$distance,$attacker['remaining_energy'],$defender['remaining_energy']);
	    			}
	    			else 
	    			{
	    				echo $battle->show_attack_options($energy_used,$_REQUEST['area'],$distance,$defender['remaining_energy'],$attacker['remaining_energy']);
	    			}
	    			
	    		}
    		}
    		else 
    		{
    			echo $battle->show_attack_options();
    		}
    		// $speed_result = $battle->speed_test(USER_ID,$opponent);
    		
    	}
    break;


    
    default:
        echo "<h1>$estab_name -&gt; Entrance</h1>";
        
        echo "<p>Welcome to the $estab_name.";       

        echo "<h2>Notice Board</h2>";
        echo '<BR><li>Today: <i>No upcoming events</i></li>';
        echo '<li>Tomorrow: <b>Clay vs Ali</b> <small>(9 registered fights)</small></li>';
        echo '<BR><BR>';

        echo 'What do you want to do here today?';

        $options = array();
        $options['/psearch/lanista/'] = 'Find a Manager at the Lanista Lounge';
        $options['/gladiator/games/apply/'] = 'Apply for main event';
        echo player_options($options);

        echo "<h2>Train</h2>";
        $options = array();
        $options['/gladiator/train/workout/'] = 'Workout';
        $options['/gladiator/fight/animals/'] = 'Fight with animals';
        $options['/gladiator/fight/people/'] = 'Fight with others';
        echo player_options($options);

        echo "<h2>Armory</h2>";
        $options = array();
        $options['/gladiator/arm/train/'] = 'Learn how to use weapons';
        $options['/gladiator/arm/immob/'] = 'Pick weapons to use';
        echo player_options($options);

        echo "<h2>Library</h2>";
        $options = array();
        $options['/gladiator/lib/stats/self/'] = 'View battle stats (personal)';
        $options['/gladiator/lib/stats/public/'] = 'View battle stats (all)';
        $options['/gladiator/lib/history/self/'] = 'View battle history (personal)';
        $options['/gladiator/lib/history/public/'] = 'View battle history (all)';
        echo player_options($options);
        
        echo "<h2>Acta Diurna (Newspaper: Gladiator Section)</h2>";
        $options = array();
        $options['/newspaper/yesterday/gladiator/'] = 'Yesterday';
        $options['/newspaper/today/gladiator/'] = 'Today';
        echo player_options($options);

        echo '</p>';
}

?>